﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Engine
{
    public class Entity : Drawn, iCanBeOverlayed
    {

        /// <summary>
        /// Contains the events of the entity
        /// </summary>
        public Dictionary<String, Event> events = new Dictionary<String, Event>();

        /// <summary>
        /// Contains the overlays on the entity
        /// </summary>
        protected Dictionary<String, Effects.Overlay> _overlays = new Dictionary<String, Effects.Overlay>();

        /// <summary>
        /// Creates an entity based on a texture and positionSize
        /// </summary>
        /// <param name="texture">Texture to be drawn on the screen</param>
        /// <param name="positionSize">The position and size of the entity</param>
        public Entity(Texture2D texture, Rectangle positionSize)
        {
            this._texture = texture;
            this._positionSize = positionSize;
        }

        /// <summary>
        /// Triggered when the update method of the game is launched
        /// </summary>
        /// <param name="gameTime">Time elapsed since last pass</param>
        public void Update(GameTime gameTime, Input.Mouse mouse)
        {

            //Loop the events
            foreach(Event e in this.events.Values)
            {
                e.Update(gameTime, mouse);
            }

        }

        /// <summary>
        /// Draws the entity on the screen
        /// </summary>
        /// <param name="spriteBatch">spriteeBatch on which to draw the entity</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            if (this._visible == false) { return; }
            
            spriteBatch.Draw(this._texture, this._positionSize, new Color(1f, 1f, 1f, 0.5f));
        }

        /// <summary>
        /// Returns the overlays applied on this element
        /// </summary>
        /// <remarks>Do not alter the dictionnary from here, use addOverlay or removeOverlay, other implications may survene later if you don't</remarks>
        Dictionary<String, Effects.Overlay> iCanBeOverlayed.getOverlays()
        {
            return this._overlays;
        }

        /// <summary>
        /// Adds an overlay or replaces the existing overlay
        /// </summary>
        /// <returns>True if overlay is new, false if overlay is replaced, the overlay still gets installed</returns>
        bool iCanBeOverlayed.addOverlay(String name, Effects.Overlay overlay)
        {
            if (this._overlays.ContainsKey(name))
            {
                this._overlays[name] = overlay;
                return false;
            }
            else
            {
                this._overlays.Add(name, overlay);
                return true;
            }
        }

        /// <summary>
        /// Removes an overlay
        /// </summary>
        /// <returns>True if overlay existed and was removed, false if overlay didn't exist</returns>
        bool iCanBeOverlayed.removeOverlay(String name)
        {
            if (this._overlays.ContainsKey(name))
            {
                this._overlays.Remove(name);
                return true;
            }
            else
            {
                return false;
            }
        }

    }
}
